package com.ad.pet.app.splash;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.support.v4.view.RxViewPager;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.DefaultPagerAdapter;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.data.utils.SystemUitl;
import com.ad.pet.common.structure.key.TokenKey;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.BaseActivity;

import java.util.Arrays;

import javax.inject.Inject;

import me.relex.circleindicator.CircleIndicator;
import rx.functions.Action1;

public class SplashActivity extends BaseActivity {
    //    private Button mBtnStart;
    private ViewPager mViewPager;
    private CircleIndicator mIndicator;
    @Inject
    RxDisposable mRxDisposable;
    private Integer[] mRes = new Integer[]{R.drawable.ic_welone, R.drawable.ic_weltwo,
            R.drawable.ic_welthree};
    private ImageView mImageView;
//    private Animation mAnimation;
    //    @BindArray(R.array.splash_title)
//    String[] mStringTitle;
//    @BindArray(R.array.splash_describe)
//    String[] mStringDescribe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        mAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.anim_scale_big);
//        mAnimation.setFillAfter(true);
    }

    @Override
    protected boolean isStatusBarTransparent() {
        return true;
    }

    @Override
    protected void initData() {
        mViewPager.setAdapter(new DefaultPagerAdapter<Integer>(Arrays.asList(mRes), R.layout.layout_splash_page) {
            @Override
            public void convert(View view, Integer item, final int position) {
                mImageView = view.findViewById(R.id.image_start);
                mImageView.setImageResource(mRes[position]);


                TextView tvTitle = view.findViewById(R.id.tv_splash_title);
                TextView tvDes = view.findViewById(R.id.tv_splash_describe);
                Button btn = view.findViewById(R.id.btn_start);
                ImageView ivFront = view.findViewById(R.id.iv_front);
//                switch (position){
//                    case 0:
////                        mImageView.startAnimation(mAnimation);
//                        ivFront.setImageResource(R.drawable.ic_s_one);
//                        break;
//                    case 1:
//
//                        ivFront.setImageResource(R.drawable.ic_s_tow);
//                        break;
//                    case 2:
//
//                        ivFront.setImageResource(R.drawable.ic_s_three);
//                        break;
//                    case 3:
//                        ivFront.setImageResource(R.drawable.ic_s_four);
//                        break;
//
//                }
                if (position == 2) {
                    btn.setVisibility(View.VISIBLE);
                } else {
                    btn.setVisibility(View.GONE);
                }
                mImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position == 2) {
                            Navigation.goMainActivity(mActivity);
                            finish();
                        }
                    }
                });

                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position == 2) {
                            Navigation.goMainActivity(mActivity);
                            finish();
                        }
                    }
                });
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mIndicator.setViewPager(mViewPager);
        //保存当前版本到Sp
        SpUtil.getInstance().putString(TokenKey.VERSION, SystemUitl.getVersionName(mActivity));

    }

    @Override
    protected void initEvents() {
//        mBtnStart.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String userId = SpUtil.getInstance().getString(TokenKey.USER_ID);
//                final boolean b = !TextUtils.isEmpty(userId);//是否已经登录
//                Navigation.goMainActivity(mActivity);
//                finish();
//                if (b) {
////                    Navigation.goMainActivity(mActivity);
//                } else {
////                    Navigation.goLoginActivity(mActivity);
//                }
//            }
//        });
        mRxDisposable.add(RxViewPager.pageSelections(mViewPager).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                if (integer == mRes.length - 1) {
//                    mIndicator.setVisibility(View.GONE);
//                    mBtnStart.setVisibility(View.GONE);
                } else {
//                    mIndicator.setVisibility(View.VISIBLE);
//                    mBtnStart.setVisibility(View.VISIBLE);
                }
//                mIndicator.setVisibility(View.GONE);
            }
        }));

    }

    @Override
    protected void initViews() {
        mViewPager =  findViewById(R.id.viewpager);
        mIndicator = findViewById(R.id.indicator);
        mIndicator.setVisibility(View.GONE);
//        mBtnStart = findViewById(R.id.btn_start);
        TextView img_start = findViewById(R.id.img_start);
        img_start.setVisibility(View.GONE);
        img_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.goMainActivity(mActivity);
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        mRxDisposable.clear();
        super.onDestroy();
    }

    @Override
    public void initInject() {
        DaggerSplachComponet.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build().inject(this);
    }


    @Override
    protected int layoutResId() {
        return R.layout.activity_splash;
    }

    protected BasePresenter getPresenter() {
        return null;
    }
}
